<template>
  <div class="model-panel-body">
    <!-- 条件栏 -->
    <PageFilter
      v-model:query="filterInfo.query"
      :filter-list="filterInfo.list"
      :listTypeInfo="listTypeInfo"
    >
    <template v-slot:filter-fill>
      <el-checkbox
        v-model="fill"
        label="库存大于0"
        border
        @change="changeCheck"
      ></el-checkbox>
    </template>
    
    </PageFilter>
    <!-- 表格 -->
    <PageTable
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="productsStorageProductPage"
      :tabIndex="true"
      :query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      rowKey="extId"
      :summary="false"
      :mergeData="tableInfo.mergeData"
      :mergeFiled="tableInfo.mergeFiled"
      :handle="tableInfo.handle"
      :isDynamic="true"
      @handleEvent="handleEvent"
      ref="multipleTable"
    >
      <template v-slot:col-licenseNumber="scope">
        <p>{{ scope.row.licenseNumber || '-' }}</p>
        <p>{{ scope.row.brandName || '-' }}</p>
      </template>
      <template v-slot:col-commonName="scope">
        <p>{{ scope.row.commonName || '-' }}</p>
        <p>{{ scope.row.productCode || '-' }}</p>
        <p>{{ scope.row.alias || '-' }}</p>
      </template>
      <template v-slot:col-type="scope">
        <p>{{ scope.row.productTypeValue || '-' }}</p>
        <p>{{ scope.row.codeTypeValue || '-' }}</p>
        <p>{{ scope.row.productCategoryValue || '-' }}</p>
      </template>
      <template v-slot:col-standard="scope">
        <p>{{ scope.row.standard || '-' }}</p>
        <p>{{ scope.row.unitName || '-' }}</p>
      </template>
    </PageTable>
    <ChosePrinter
      :visible="printerInfo.visible"
      @callBack="handlePrinterCallBack"
    />
  </div>
</template>

<script setup name="codeBarPrint">
import {
  getDepotAll,
  productsStorageProductPage,
  productCodePrint,
} from '@/api/core'
const { proxy } = getCurrentInstance()
const emits = defineEmits()
const printerInfo = ref({
  visible: false,
  data: {},
})
const listTypeInfo = ref({
  productCodeTypeList: [
    { key: '无码', value: 1 },
    { key: '系统码', value: 2 },
    { key: '厂家码', value: 3 },
    { key: '批次码', value: 4 },
  ],
})
const fill = ref(false)
// 过滤相关配置
const filterInfo = ref({
  query: {
    sort: 'product_id desc',
    searchType: 'product',
    searchField: 'productId',
    enabledDepotAuth: 'depotId',
    stockQtyGreater: '0',
    differentPriceMult: true,
    greaterThanZero: '',
    // needPrintType: "system",
    depotId: null,
  },
  list: [
    {
      type: 'slip-select',
      label: '仓库',
      value: 'depotId',
      api: getDepotAll,
      query: {
        depotAuth: 'id',
        useTypeId: '1',
        status: '1',
      },
      clearable: false,
      event: search,
    },
    {
      type: 'select',
      label: '类型',
      value: 'productCodeType',
      event: search,
      list: 'productCodeTypeList',
      clearable: true,
    },
    {
      type: 'search',
      label: '名称/别名/编码/助记符搜索',
      value: 'keyword',
      event: search,
    },
    {
      type: 'slot',
      value: 'fill'
    }
    // { type: "button", label: "搜索", event: this.search }
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  selections: [],
  summaryFieldList: [
    { value: 'stockQty', formatter: proxy.$fn.qtyFormatter },
    { value: 'sumPrice', formatter: proxy.$fn.TripartiteMethod },
    { value: 'frozenQty', formatter: proxy.$fn.qtyFormatter },
    { value: 'pendStockQty', formatter: proxy.$fn.qtyFormatter },
  ],
  mergeFiled: ['commonName', 'type', 'standard', 'licenseNumber'],
  mergeData: {},
  fieldList: [
    {
      label: '通用名称\n编码\n别名',
      value: 'commonName',
      type: 'slot',
      minWidth: '220px',
    },
    {
      label: '产品类型\n条码类型\n产品分类',
      value: 'type',
      type: 'slot',
      minWidth: '150px',
    },
    { label: '规格\n计量单位', value: 'standard', type: 'slot' },
    {
      label: '证号\n生产厂家',
      value: 'licenseNumber',
      type: 'slot',
      minWidth: '220px',
    },
  ],
  singleProductDialog: {
    visible: false,
    query: {},
  },
  handle: {
    fixed: 'right',
    label: '操作',
    width: '80',
    btList: [
      {
        label: '打印产品码',
        event: printProductCode,
        show: true,
      },
    ],
  },
})

watch(
  () => tableInfo.value.data,
  (val) => {
    tableInfo.value.data.forEach((row) => (row.sumPrice = row.qty * row.price))
    tableInfo.value.mergeData = proxy.$fn.getMergeData(val)
  }
)
function changeCheck(){
  filterInfo.value.query.greaterThanZero = fill.value ? '1' : ''
  search()
}
// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function initDepotList() {
  const params = {
    depotAuth: 'id',
    useTypeId: '1',
    status: '1',
  }
  getDepotAll(params).then((resp) => {
    if (resp && resp.success && resp.data && resp.data.length) {
      filterInfo.value.query.depotId = resp.data[0].id
      getList()
    }
  })
}
function onPrintQRcode(row) {
  emits('handlePrintQRcode', row)
}
function onIndividualTableInfoDatails(data) {
  tableInfo.value.data.map((item) => {
    if (
      item.productId === data.table.productId &&
      item.validityDate === data.table.validityDate &&
      item.batchNumber === data.table.batchNumber &&
      item.productionDate === data.table.productionDate
    ) {
      item.selections = data.selections

      proxy.$refs['multipleTable'].toggleRowSelection(
        item,
        !!data.selections.length
      )

      return item
    }
  })
}
// 搜索
function search() {
  // 重置分页
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.value.selections = [...data]
      emits('callback', {
        selections: tableInfo.value.selections,
        query: filterInfo.value.query,
      })
      break
    case 'list':
      tableInfo.value.data = data.map((item) => {
        return {
          ...item,
          extId: item.id + Math.random(),
        }
      })
      break
  }
}
function printProductCode(row) {
  let data = []
  if (row && row.id) {
    data.push(row.id)
  } else {
    data = tableInfo.value.selections.map((item) => item.id)
  }
  productCodePrint(data).then((res) => {
    if (res && res.success) {
      printerInfo.value.visible = true
      printerInfo.value.data = res.data
    }
  })
}
function handlePrinterCallBack(printer) {
  if (printer !== false) {
    proxy.$print.buildRenderBarCode(printerInfo.value.data, printer)
  }
  printerInfo.value.visible = false
}

onMounted(() => {
  initDepotList()
})
</script>

<style scoped lang="scss">
:deep(.page-table > .el-table) {
  overflow: hidden;
}
</style>
